package com.infoauto.opc.gateway.opc.model;


import com.infoauto.opc.gateway.model.gtm4.Equipment;
import com.infoauto.opc.gateway.model.gtm4.EquipmentAbnormal;
import com.infoauto.opc.gateway.opc.LogMessageHandler;
import com.infoauto.opc.gateway.opc.MessageHandler;
import com.infoauto.opc.gateway.opc.client.OpcConnection;
import com.infoauto.opc.gateway.opc.utils.SpringContextUtils;
import com.infoauto.opc.gateway.service.EquipmentAbnormalService;
import com.infoauto.opc.gateway.service.EquipmentService;
import lombok.Getter;
import lombok.Setter;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.infoauto.opc.gateway.opc.utils.DataTypeConversionUtils.DataTypeConversionToString;

/**
 * 订阅的节点
 * */
@Getter
@Setter
public class SubscribeNode {

    /**
     * 当前节点的OPC连接
     * */
    private OpcConnection opcConnection;

    private String serverID;

    /***
     * 订阅的编号，只有订阅成功后才会生成
     * */
    private int subscriptID;

    /**
     * opc节点编号
     * */
    private int nodeID;

    /**
     * OPC地址
     * */
    private String nodePath;

    /**
     * 当前节点的值
     * */
    private Variant nodeValue;

    /**
     * 订阅状态，代表是否订阅，true是已经订阅，false是未订阅
     * **/
    private boolean isSubscribe;

    /**
     * 节点读取方式，0表示订阅，1轮循
     * **/
    private int readType;

    private String groupName;

    private String nodeName;

    /**
     * 关联的MQTT节点集合
     * **/
    private Map<Integer, MqttInterfaceNode> associatedINodes;

    /**
     * 第三方接口集合
     * */
    private Map<Integer, WebInterfaceNode> webInterfaceNodes;

    /**
     * 订阅的节点
     * **/
    private UaMonitoredItem monitoredItem;
    @Autowired
    private MessageHandler messageHandler;

    @Autowired
    private SpringContextUtils springContextUtils;
    /**
     * 发布数据
     * **/
    public void publishData() {
        // MQTT推送的数据
        if (this.associatedINodes!=null){
            for (MqttInterfaceNode in:this.associatedINodes.values()){
                in.setNodeValue(this.nodeValue);
                MqttInterface oi= in.getOpcInterface();
                MessageHandler.This().MqttProcessor(oi.getInterfaceName(), oi.toString());
                LogMessageHandler.This().LogProcessor(oi.getInterfaceName(),oi);
            }
        }

        // 推送数据给第三方接口
        if(this.webInterfaceNodes!=null){
            for (WebInterfaceNode in:this.webInterfaceNodes.values()){
                in.setTagValue(this.nodeValue.getValue().toString());
                WebInterface wi= in.getThirdInterface();
                if(wi.getWorkDirection() == 1){
                    wi.doCallBack();
                }
            }
        }
    }

    /**
     * 写入OPC
     * */
    public boolean writeNodeValue(Variant var) {
        this.nodeValue = var;
        boolean flag = false;
        if(this.opcConnection!=null){
            flag = this.opcConnection.writeNodeValue(this.nodePath,this.nodeValue);
        }
        return flag;
    }
    /**
     * 读OPC的值
     */
    public Variant readNodeValue(String nodePath){
        Variant variant =  this.opcConnection.readNodeValue(nodePath);
        return variant;
    }
}